package mango.functions;

public final class FuncString {
    public static boolean isBlank(String str) {
        if (str == null || str.trim().length() == 0) {
            return true;
        }
        return false;
    }

    public static String null2Blank(String str) {
        if (isBlank(str)) {
            return "";
        }
        return str;
    }

    public static String firstUpper(String str) {
        StringBuilder ret = new StringBuilder();
        if (str != null && str.length() > 0) {
            ret.append(str.substring(0, 1).toUpperCase()).append(str.substring(1));
        }
        return ret.toString();
    }

    public static String firstLower(String str) {
        StringBuilder ret = new StringBuilder();
        if (str != null && str.length() > 0) {
            ret.append(str.substring(0, 1).toLowerCase()).append(str.substring(1));
        }
        return ret.toString();
    }

    public static String margeString(Object[] obj, String token) {
        if (obj == null || obj.length == 0) {
            return "";
        }
        String t = token;
        if (t == null) {
            t = "";
        }
        StringBuilder ret = new StringBuilder();
        for (Object o : obj) {
            ret.append(o.toString()).append(token);
        }
        ret.delete(ret.length() - token.length(), ret.length());
        return ret.toString();
    }

    public static boolean containIgnoreCase(String str1, String str2) {
        if (isBlank(str1) || isBlank(str2)) {
            return false;
        }
        String tem1 = str1.toUpperCase();
        String tem2 = str2.toUpperCase();
        if (tem1.equals(tem2) || tem1.contains(tem2)) {
            return true;
        }
        return false;
    }
    
    public static String encodeString(String val) {
        Object ret = encodeIfString(val);
        if (ret == null) {
            return null;
        }
        return ret.toString();
    }

    public static Object encodeIfString(Object val) {
        if (val == null) {
            return null;
        }
        if (val instanceof String) {
            String ret = val.toString().replaceAll("\"", "&quot;").replaceAll("<", "&lt;")
                    .replaceAll(">", "&gt;");
            return ret;
        }
        return val;
    }

    public static String decodeString(String val) {
        if (val == null) {
            return null;
        }
        String ret = val.toString().replaceAll("&quot;", "\"").replaceAll("&lt;", "<")
                .replaceAll("&gt;", ">");
        return ret;
    }
    
    public static String reverse(String str) {
        if (str == null) {
            return null;
        }
        return str.length() > 0 ? reverse(str.substring(1)) + str.charAt(0) : "";
    }
    
}
